<?php
class Admin_ProductController extends Zend_Controller_Action{
	public function init(){
	parent::init();
	}
	public function indexAction(){
		$pro=new Admin_Model_Product();
		$flag=0;
		if(isset($_POST['cmd']))
		{
			if($_POST['cmd']=="delete")
			{
				$pro->DeleteMoreProduct();				
			}
			if($_POST['cmd']=="saveOrders")
			{
				$pro->SaveOrder();				
			}
			if($_POST['cmd']=="changeCategory")
			{
				$flag=1;
				$categoryId=$_POST['categoryId'];
				$pro1=$pro->getCategoryProducts($categoryId,$this->_getParam('page'));	
				$this->view->pro=$pro1['data'];	
				$this->view->paginator=$pro1['paginator'];
				$this->view->categoryId=$categoryId;					
			}		
			if($_POST['cmd']=="publish")
			{
				$pro->publish();
			}	
		}			
			if($flag==0)
			{
				$pro2=$pro->getRandomProducts($this->_getParam('page'));
				$this->view->pro=$pro2['data'];
				$this->view->paginator=$pro2['paginator'];
			}
		$category=new Admin_Model_ProductCategory();
		$this->view->categoryList=$category->getIdNameCategory();
		
	}	
	public function createAction()
	{
		$pro=new Admin_Model_Product();		
		if ($this->_request->isPost()) 
		{						
			/*$product=array(
			"productId"=>$_POST["productId"],
			"ordering"=>$_POST["ordering"],
			"productName"=>$_POST["productName"],
			"summary"=>$_POST["summary"],
			"description"=>$_POST["description"],
			"status"=>$_POST["status"],
			"imageURL"=>$_POST["imageURL"],
			"creationTime"=>$_POST['creationTime']);*/
			if(isset($_POST['cmd']))
			{
				if($_POST['cmd']=="save")
				{
					if(empty($_POST["productId"]))
					{
						if($pro->CreateProduct())
						{
							$this->_redirect("/admin/product/index");						
							exit;
						}
						else
						{	
							$this->view->errorList=$pro->getErrorMsg();			
							$this->view->product = $_POST;				
						}
					}
					else {
							if($pro->UpdateProduct())
							{
								$this->_redirect("/admin/product/index");						
								exit;
							}
							else
							{	
								$this->view->errorList=$pro->getErrorMsg();			
								$this->view->product = $_POST;				
							}
						
					}
				}
			}
		}
		else
		{
			$id = $this->_getParam("id");
			
			if(!empty($id))
			{				
				$this->view->dd=$id;
				$temp=$pro->getOneProduct($id);
				$this->view->product = $temp[0];
			}
		}
		
		$category=new Admin_Model_ProductCategory();
		$this->view->categoryList=$category->getIdNameCategory();
	}
	public function deleteAction()
	{
		$product=new Admin_Model_Product();								
	
		$idpro=$this->_getParam("id");
		//$this->view->pp=$idpro;
		if($product->DeleteProduct($idpro))
		{
			$this->_redirect("/admin/product/index");
		}
		else
		{
			echo "delete Failed!";
			//$this->_redirect("/admin/error/error");
			$this->_redirect("/admin/product/index");
		}		
			
	}	
}